/**
 * 安卓白金英雄坛制作组 <br>
 * 文件名：FreeStatus.java <br>
 * 创建时间：2013-8-5 下午6:00:57 <br>
 * 所属项目：GmudEX <br>
 *
 * @author 12548 <br>
 */
package lostland.gmud.exv2.battle.proc;

import com.google.gson.Gson;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.orhanobut.logger.Logger;

import org.json.JSONException;
import org.json.JSONObject;

import lostland.general.network.GmudHttp;
import lostland.general.network.GmudRequestListener;
import lostland.general.network.GmudRestUrl;
import lostland.gmud.exv2.Game;
import lostland.gmud.exv2.GameConstants;
import lostland.gmud.exv2.Gmud;
import lostland.gmud.exv2.battle.BattleScreen;
import lostland.gmud.exv2.battle.BattleStateInfo;
import lostland.gmud.exv2.battle.ControlScreen;
import lostland.gmud.exv2.battle.StuntData;
import lostland.gmud.exv2.battle.ViewScreen;
import lostland.gmud.exv2.battle.netbattle.NetworkMode;
import lostland.gmud.exv2.battle.netbattle.WaitingForActionScreen;
import lostland.gmud.exv2.battle.proc.stunt.FeiYingZhaoHuan;
import lostland.gmud.exv2.data.Npc;
import lostland.gmud.exv2.data.Room;
import lostland.gmud.exv2.ui.NotificationScreen;

/**
 * 类名：FreeStatus <br>
 * 说明：战斗自由状态，可转到玩家操纵屏或NPC自动操作流程
 *
 * @author 12548
 */
public class FreeStatus extends Status {


	public FreeStatus(Npc zdp, Npc bdp) {
		super(zdp, bdp);
	}

	public static int nround = -1;


	/*
	 * （非 Javadoc）
	 * @see lostland.gmud.exv2.battle.proc.Status#execute()
	 */
	@Override
	public void execute() {
		Logger.v("doing");


		if (zdp.getBuff(7) > 0) {
			new FeiYingZhaoHuan(zdp, bdp).pushToBattle();
		}

		if (zdp == Gmud.mc && !BattleScreen.getInstance().trust) {
			new ControlScreen().pushToGame();
			Logger.v("控制界面已设置");
			return;
		} else if (zdp.getDz() > 0) {
			zdp.setDz(zdp.getDz() - 1);
// GmudWorld.bs.setStatus(new RoundOverStatus());

			new ViewScreen(zdp.name + "现在呆若木鸡！(剩余回合" + zdp.getDz()
					+ ")").pushToGame();
// Gmud.bs.push(new DummyStatus());

			return;
		} else if (zdp != Gmud.mc && BattleScreen.getInstance().networkMode == NetworkMode.HOST) {

			final RequestParams initBattleParams = GmudHttp.initBattleParams();
			initBattleParams.addBodyParameter("msg", "");
			initBattleParams.addBodyParameter("hit", "3");
			initBattleParams.addBodyParameter("stateinfo", new Gson().toJson(BattleStateInfo.getState()));
			new GmudHttp().postLocked(GmudRestUrl.HostPostMsg, initBattleParams, new GmudRequestListener() {

				@Override
				public void onSucc(ResponseInfo<String> info) {
					try {
						JSONObject jo = new JSONObject(info.result);
						String s = jo.getString(GmudHttp.KEY_STATE);

						if (s.equals(GmudHttp.STATE_OK)) {
							new WaitingForActionScreen().pushToGame();
						} else {
							Game.getInstance().popToRootScreen();
							Logger.e("未知错误：" + jo.getString(s));
							new NotificationScreen("发生了未知错误！").pushToGame();
						}
					} catch (JSONException e) {
						if (Gmud.DEBUG) {
							e.printStackTrace();
						}
						Game.getInstance().popToRootScreen();
						new NotificationScreen("发生了未知错误！").pushToGame();
					}


				}

				@Override
				public void onFail(HttpException error, String msg) {
					Game.getInstance().popToRootScreen();
					new NotificationScreen("网络连接错误！").pushToGame();
				}
			});

			return;
		} else if(zdp.getDrug2() > 0 && zdp.getHp() * 2 <= zdp.getAttr(Room.ATTR_MAXHP) && zdp.getFp() >= zdp.getHp()) {

			String pf = "";

			if (zdp.haveTalent(124) && Gmud.rand.nextBoolean()) {  // 天赋效果124
				zdp.setDrug2(zdp.getDrug2() + 1);
				pf += "【至隐无名】";
			}


			zdp.setDrug2(zdp.getDrug2() - 1);
			zdp.cure((int) (zdp.getAttr(Room.ATTR_MAXHP) * 0.5f));

			new ViewScreen(pf + zdp.name + "使用了【生肌膏】，恢复了部分伤势！").pushToGame();
			pushToBattle();
// Gmud.bs.push(new DummyStatus());
			return;
		} else if(zdp.getDrug1() > 0 && zdp.getHp() <= (int) (zdp.getAttr(Room.ATTR_MAXHP) * 0.75f)
				&& zdp.getFp() >= (int) (zdp.getAttr(Room.ATTR_MAXHP) * 0.25f)) {

			String pf = "";

			if (zdp.haveTalent(124) && Gmud.rand.nextBoolean()) // 天赋效果124
			{
				zdp.setDrug1(zdp.getDrug1() + 1);
				pf += "【至隐无名】";
			}

			zdp.setDrug1(zdp.getDrug1() - 1);
			zdp.cure((int) (zdp.getAttr(Room.ATTR_MAXHP) * 0.25f));
			new ViewScreen(pf + zdp.name + "使用了【金创药】，恢复了部分伤势！").pushToGame();
			pushToBattle();
// Gmud.bs.push(new DummyStatus());
			return;
		}


		double basicSpecialMovementRate = 0.2 * Game.getInstance().data.game_difficulty;
		int aiXiqiCooldown = 4 - Game.getInstance().data.game_difficulty;

		nround++;

		int avStunts[] = new int[0];

		for (int i = 0; i < StuntData.getInstance().size(); i++) {
			if (StuntScreen.canuse(zdp, i)) {
				avStunts = Gmud.push_back(avStunts, i);
				Logger.i(StuntData.getInstance(i).name + " is avalible");
			}
		}

		Logger.i(avStunts.length + " Stunts avalible in total");

		if (avStunts.length > 0 && Math.random() < basicSpecialMovementRate && zdp.isStuntAvaliable()) {
			// NPC发动绝招
			StuntScreen.process(zdp, bdp, avStunts[(int) (Math.random() * avStunts.length)]);
			nround = -1;
// GmudGame.getInstance().pushScreen(new ViewScreen(GmudGame.getInstance()));
// Gmud.bs.push(new DummyStatus());
		} else if (zdp.getGauge() >= GameConstants.XIQI_GAUGE_COST
				&& zdp.getSp() < zdp.getHp()
				&& zdp.getFp() > 0
				&& ((nround >= aiXiqiCooldown && Math.random() < basicSpecialMovementRate) || zdp.getSp() * 3 <= zdp.getHp())) {
//			pushToBattle();
			new DummyStatus().pushToBattle();
			BattleScreen.getInstance().xiqiprocess(zdp, bdp);
			nround = -1;
		} else if (zdp.getGauge() >= GameConstants.ATTACK_GAUGE_COST) {
			zdp.addGauge(-GameConstants.ATTACK_GAUGE_COST);
			BattleScreen.getInstance().atkprocess(zdp, bdp, null, null);
		} else {
			zdp.addGauge(2);
			new ViewScreen(BattleScreen.bsp(zdp, bdp,
					"$N站立不动，暗中积蓄力量。")).pushToGame();
		}


	}

}
